#include <fintrf.h>
C
C get_last_valfg.FOR - Gateway function for get_last_val.FOR
C API here: http://www.mathworks.com/access/helpdesk/help/techdoc/index.html?/access/helpdesk/help/techdoc/apiref/bqoqnz0.html&http://www.mathworks.com/support/tech-notes/1600/1605.html
C
      SUBROUTINE mexFunction(NLHS, PLHS, NRHS, PRHS)
C-----------------------------------------------------------------------
C     (pointer) Replace integer by integer*8 on 64-bit platforms
C
      mwPointer PLHS(*), PRHS(*)
      mwsize NLHS, NRHS

      mwPointer mxCreateDoubleMatrix, mxGetPr

!     Functions to get matrix sizes
      mwSize mxGetM, mxGetN
C
C KEEP THE ABOVE SUBROUTINE, ARGUMENT, AND FUNCTION DECLARATIONS FOR USE
C IN ALL YOUR FORTRAN MEX FILES.
C---------------------------------------------------------------------
C
      mwPointer last_x_p, x_p, cur_x_pos_p, length_x_p

C-----------------------------------------------------------------------
C

      mwSize m, n, size
      DOUBLE PRECISION :: last_x
      REAL*8 :: length_x_real, cur_x_pos_real
      INTEGER :: length_x, cur_x_pos
      REAL*8, dimension(10) :: x

      character*120 :: line

C
C CHECK FOR PROPER NUMBER OF ARGUMENTS
C

!      write(line,*) "NRHS=", NRHS," <--"
!     CALL mexPrintf(line)
      IF (NRHS .NE. 3) THEN
        CALL mexErrMsgTxt('get_last_val requires three input arguments')
      ELSEIF (NLHS .GT. 1) THEN
        CALL MEXERRMSGTXT('get_last_val requires one output argument')
      ENDIF

C     Get the size of the input array
      m = mxGetM(prhs(1))
      n = mxGetN(prhs(1))
      size = m*n

!      write(line,*), "m=", m, "n=", n
!      call mexPrintf(line)
C     Make sure the input isn't exceeding constraints
      if (m .gt. 1) then
        call mexErrMsgTxt("X input must me a vector")
      elseif (size .gt. 100) then
        call mexErrMsgTxt("X vector must be <= 100 elements in length")
      endif

C     Ensure the array is numeric (not strings)
      if (mxIsNumeric(prhs(1)) .eq. 0) then
        call mexErrMsgTxt('X vector must be numeric')
      endif

C
C CREATE A MATRIX FOR RETURN ARGUMENT
C
      PLHS(1) = mxCreateDoubleMatrix(1,1,0)

!     Assign pointers to the various parameters
      ! Output
      last_x_p    = mxGetPr(PLHS(1))

      ! Input
      x_p         = mxGetPr(PRHS(1))
      cur_x_pos_p = mxGetPr(PRHS(2))
      length_x_p  = mxGetPr(PRHS(3))

C COPY RIGHT HAND ARGUMENTS TO LOCAL ARRAYS OR VARIABLES
      CALL mxCopyPtrToReal8(length_x_p, length_x_real, 1)
      length_x = int(length_x_real)
      write(line,*), "length_x=", length_x,"\n"
      call mexPrintf(line)

      CALL mxCopyPtrToReal8(x_p, x, 10)
      write(line,*), "x=", x,"\n"
      call mexPrintf(line)

      CALL mxCopyPtrToReal8(cur_x_pos_p, cur_x_pos_real, 1)
      cur_x_pos = int(cur_x_pos_real)
      write(line,*), "cur_x_pos=", cur_x_pos, "\n"
      call mexPrintf(line)

C
C DO THE ACTUAL COMPUTATIONS IN A SUBROUTINE
C       CREATED ARRAYS.  
C
      CALL get_last_val(last_x, x, cur_x_pos, length_x)
C
C COPY OUTPUT WHICH IS STORED IN LOCAL ARRAY TO MATRIX OUTPUT
      CALL mxCopyReal8ToPtr(last_x, last_x_p, 1)
C
      RETURN
      END

      include "get_next_val.F"
      include "get_last_val.F"
